package com.glasses.business.fragment;

import android.os.Bundle;
import android.view.View;

import com.glasses.business.adapter.GlassPairAdapter;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.BaseListFragment;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.GlassDetail;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/9
 * Version：1.0
 */

public class GlassPairFragment extends BaseListFragment {
    private List<GlassDetail> list;
    private GlassPairAdapter glassPairAdapter;
    private GlassDetail glassDetail;

    @Override
    protected boolean isHaveHead() {
        return true;
    }

    @Override
    protected void setHeadViews() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        setTitleTv("镜片列表");
    }


    public void onEventMainThread(BussEvent event) {
        if (event.getState() == BussEvent.Product1) {
//            BussEvent event1 = new BussEvent(BussEvent.Product1);
//            event1.setMessage(glassDetail);
//            EventBus.getDefault().post(event1);
            MyApplication.getInstance().manager.onBackPressed();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected MyBaseAdapter getAdapter() {
        glassPairAdapter = new GlassPairAdapter(getActivity(), null);
        glassPairAdapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                glassDetail = (GlassDetail) data.get(position);
                Bundle bundle = new Bundle();
                bundle.putInt("id", glassDetail.getId());
                bundle.putInt("type", 1);
                open(new GlassDetailFragment(), bundle);
            }
        });
        return glassPairAdapter;
    }

    @Override
    protected int getLineNum() {
        return 0;
    }

    @Override
    public void initData() {
        loadDatas(true);
    }

    private void loadDatas(final boolean isRefush) {
        Tools.showDialog(getActivity());
        NetUtils.getInstance().product1List(pageNum, PAEG_SIZE, new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
                mRefeshLy.hideAll();
                list = (List<GlassDetail>) model.getModelList();
                if (isRefush) {
                    glassPairAdapter.clear();
                }
                glassPairAdapter.append(list);
                if (list == null || list.size() < 10) {
                    if (isRefush && (list == null || list.size() == 0)) {
//                        mRefeshLy.showEmptyView();
                        mRefeshLy.showFailView();
                    }
                    mRecyclerview.setLoadingMoreEnabled(false);
                } else {
                    mRecyclerview.setLoadingMoreEnabled(true);
                }
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                //                mRefeshLy.hideAll();
                mRefeshLy.showFailView();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
            }
        }, GlassDetail.class);
    }

    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public void onRefresh() {
        pageNum = 0;
        loadDatas(true);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        loadDatas(false);
    }

    @Override
    public void onRetryClick() {
        pageNum = 0;
        loadDatas(true);
    }
}
